var autoCompleteDiv;

function setAutoComplete(input){
	input.changed=function(e){
		e=getEventObject(e);
		var el=getEventSource(e);
		el.removeAutoComplete();
		var action=ROOT_PATH+ACTION_PATH+"?doAction="+input.getAttribute("action");
		if(el.value.length>1){
			var loader=new xmlLoader();
			var theInput=el;
			loader.onload=function(e){
				theInput.setAutoComplete(this.textLoaded);
			}
			loader.load(action+"&"+el.name+"="+el.value);
		}
	}
	input.setAutoComplete=function(txt){
		this.setAutoCompleteDiv(txt);
	}
	input.setAutoCompleteDiv=function(txt){
		if(!autoCompleteDiv){
			autoCompleteDiv=document.createElement("DIV");
			document.body.appendChild(autoCompleteDiv);
			autoCompleteDiv.className="autoCompleteDiv";
		}
		autoCompleteDiv.innerHTML=txt;
		var pos=getAbsolutePosition(this);
		autoCompleteDiv.style.left=pos.x+"px";
		autoCompleteDiv.style.top=(pos.y+this.offsetHeight)+"px";
		autoCompleteDiv.style.minWidth=this.offsetWidth+"px";
		for(var i=0;i<autoCompleteDiv.childNodes.length;i++){
			autoCompleteDiv.childNodes[i].onmousedown=this.elementClicked;
		}
		autoCompleteDiv.caller=this;
		addListener(document,"mousedown",this.removeAutoComplete);
	}
	input.removeAutoComplete=function(){
		if(autoCompleteDiv){
			document.body.removeChild(autoCompleteDiv);
			autoCompleteDiv=null;
			removeListener(document,"keyup",this.removeAutoComplete);
		}
	}
	input.elementClicked=function(e){
		e=getEventObject(e);
		var el=getEventSource(e);
		if(MSIE){
			autoCompleteDiv.caller.value=el.innerText;
		}else{		
			autoCompleteDiv.caller.value=el.textContent;
		}
		try{
			var hidden=autoCompleteDiv.caller.parentNode.getElementsByTagName("INPUT")[1];
			hidden.value=el.getAttribute("value");
		}catch(e){}
	}
	addListener(input,"keyup",input.changed);
	
}